﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace papercut2.Data.ImageProcess
{
    //二值化类
    class Binarization
    {
        private static Bitmap pic;
        public static Bitmap Pic
        {
            get
            {
                return pic;
            }
            set
            {
                pic = value;
            }
        }
        public static Bitmap BinarizationPictrue()
        {
            Bitmap box1 = pic;
            double num;
            int i, j;
            int k;
            int[,] h = new int[1280, 1280];
            int r;
            int[] hd = new int[300];
            double[] p = new double[300];
            double[] tt = new double[300];
            double u = 0;
            double[] uu = new double[300];
            double[] w = new double[300];
            double[] b = new double[300];
            double max;
            double maxb = 0;
            int t;

            Color cc1 = Color.FromArgb(255, 255, 255);
            Color cc2 = Color.FromArgb(0, 0, 0);

            Color c = new Color();//temp for color 


            int width = box1.Width;
            int height = box1.Height;
            //读取图象颜色值
            for (i = 1; i < width; i++)
            {
                for (j = 1; j < height; j++)
                {
                    c = box1.GetPixel(i, j);
                    r = c.R;
                    h[i, j] = r;
                }
            }
            //累加图象灰度值
            for (i = 1; i < width; i++)
            {
                for (j = 1; j < height; j++)
                {
                    for (r = 0; r < 255; r++)
                    {
                        if (h[i, j] == r)
                            tt[r] = tt[r] + 1;
                    }
                }
            }
            //计算灰度概率函数
            for (t = 0; t < 255; t++)
            {
                p[t] = tt[t] / (width * height);
            }
            //计算灰度均值
            for (t = 1; t < 256; t++)
            {
                u = u + (t - 1) * p[t];
            }
            //计算灰度类均值
            for (k = 1; k < 256; k++)
            {
                uu[k] = uu[k - 1] + (k - 1) * p[k];
                w[k] = w[k - 1] + p[k];
                if (w[k] * (1 - w[k]) != 0)
                    b[k] = ((u * w[k] - uu[k]) * (u * w[k] - uu[k])) / (w[k] * (1 - w[k]));
            }
            max = b[0];
            for (k = 0; k < 255; k++)
            {
                if (b[k] >= max)
                    max = b[k];
            }
            for (k = 0; k < 255; k++)
            {
                if (b[k] == max) maxb = k;
            }
            num = maxb - 1;

            for (i = 0; i < width; i++)
            {
                for (j = 0; j < height; j++)
                {
                    c = box1.GetPixel(i, j);
                    r = c.R;
                    if (r > num) box1.SetPixel(i, j, cc1);
                    else box1.SetPixel(i, j, cc2);
                }

            }
            pic = box1;
            return box1;
        }
    }
}
